from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QAbstractItemView,\
                            QTableWidgetItem, QTableWidget, QSizePolicy, QHeaderView

class MyWorkSpace(QWidget):
    def __init__(self, widget):
        super().__init__(parent=widget)
        self.setMaximumWidth(300)
        self.setMinimumWidth(170)

        self.setObjectName("workspace")
        self._init()
        self.item_count = 0
        self.item_storage = []

    def _init(self):
        self.verticalLayout = QVBoxLayout(self)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setSpacing(0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.tableWidget = QTableWidget(self)
        sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
        self.tableWidget.setSizePolicy(sizePolicy)
        self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.tableWidget.setMinimumSize(QSize(0, 0))
        self.tableWidget.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setRowCount(0)
        self.tableWidget.resizeRowToContents(0)
        item = QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        self.tableWidget.horizontalHeader().setDefaultSectionSize(74)
        self.verticalLayout.addWidget(self.tableWidget)
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText("名称")
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText("值")

    def add_item(self, file_info):
        """ 添加item """

        name = file_info.name
        rows = file_info.rows
        cols = file_info.cols
        shape = str(rows) + "x" + str(cols)
        self.show_item(name, shape)
        self.item_storage.append(file_info)
        self.item_count += 1

    def show_item(self, name, shape):
        """ 展示文件名及其shape """
        name_item = QTableWidgetItem(name)
        name_item.setTextAlignment(Qt.AlignCenter)
        shape_item =QTableWidgetItem(shape)
        shape_item.setTextAlignment(Qt.AlignCenter)
        self.tableWidget.insertRow(self.item_count)
        self.tableWidget.setItem(self.item_count, 0, name_item)
        self.tableWidget.setItem(self.item_count, 1, shape_item)

    def del_item(self, index):
        """ 根据索引删除item """
        if index < self.item_count:
            self.tableWidget.removeRow(index)
            self.item_storage.pop(index)
            self.item_count -= 1

    def update_index(self, index):
        for i in range(self.item_count):
            display_number = self.item_storage[i].display_number
            if display_number >= index:
                self.item_storage[i].display_number = display_number - 1
            if display_number == index:
                self.item_storage[i].is_displaying = False
